<template>
    <div id="app">
        <router-view :key="$route.Index" />
    </div>
</template>

<script>
import storage from "@/plugins/storage";
import {
    getBaseSite,
    getAdminState,
    getAdminLoginUrl
} from "@/api/common.js";

export default {
    name: "App",

    mounted() {
        this.init();
        // localStorage.setItem('userInfo', '{ "id": 1, "username": "admin", "nickname": "Admin", "password": "27ca7873a8789833cdb80c5545d360af", "salt": "sUb4pt", "avatar": "/uploads/20230326/f57ebce8a72b823912904fe76eda0909.png", "email": "admin@admin.com", "mobile": "", "loginfailure": 0, "logintime": 1713837105, "loginip": "36.57.139.161", "createtime": 1491635035, "updatetime": 1713837105, "token": "fdbeab5b-c1e6-4813-ae22-62972f3a5075", "status": "normal", "role": "", "roleid": 0, "commission": "0.00" }');
        // sessionStorage.setItem('userInfo', '{ "id": 1, "username": "admin", "nickname": "Admin", "password": "27ca7873a8789833cdb80c5545d360af", "salt": "sUb4pt", "avatar": "/uploads/20230326/f57ebce8a72b823912904fe76eda0909.png", "email": "admin@admin.com", "mobile": "", "loginfailure": 0, "logintime": 1713837105, "loginip": "36.57.139.161", "createtime": 1491635035, "updatetime": 1713837105, "token": "fdbeab5b-c1e6-4813-ae22-62972f3a5075", "status": "normal", "role": "", "roleid": 0, "commission": "0.00" }');
        //  this.Cookies.setItem('userInfo', '{ "id": 1, "username": "admin", "nickname": "Admin", "password": "27ca7873a8789833cdb80c5545d360af", "salt": "sUb4pt", "avatar": "/uploads/20230326/f57ebce8a72b823912904fe76eda0909.png", "email": "admin@admin.com", "mobile": "", "loginfailure": 0, "logintime": 1713837105, "loginip": "36.57.139.161", "createtime": 1491635035, "updatetime": 1713837105, "token": "fdbeab5b-c1e6-4813-ae22-62972f3a5075", "status": "normal", "role": "", "roleid": 0, "commission": "0.00" }');
        // localStorage.setItem('admin', '{ "id": 1, "username": "admin", "nickname": "Admin", "password": "27ca7873a8789833cdb80c5545d360af", "salt": "sUb4pt", "avatar": "/uploads/20230326/f57ebce8a72b823912904fe76eda0909.png", "email": "admin@admin.com", "mobile": "", "loginfailure": 0, "logintime": 1713837105, "loginip": "36.57.139.161", "createtime": 1491635035, "updatetime": 1713837105, "token": "fdbeab5b-c1e6-4813-ae22-62972f3a5075", "status": "normal", "role": "", "roleid": 0, "commission": "0.00" }');
        // sessionStorage.setItem('admin', '{ "id": 1, "username": "admin", "nickname": "Admin", "password": "27ca7873a8789833cdb80c5545d360af", "salt": "sUb4pt", "avatar": "/uploads/20230326/f57ebce8a72b823912904fe76eda0909.png", "email": "admin@admin.com", "mobile": "", "loginfailure": 0, "logintime": 1713837105, "loginip": "36.57.139.161", "createtime": 1491635035, "updatetime": 1713837105, "token": "fdbeab5b-c1e6-4813-ae22-62972f3a5075", "status": "normal", "role": "", "roleid": 0, "commission": "0.00" }');
        //  this.Cookies.setItem('admin', '{ "id": 1, "username": "admin", "nickname": "Admin", "password": "27ca7873a8789833cdb80c5545d360af", "salt": "sUb4pt", "avatar": "/uploads/20230326/f57ebce8a72b823912904fe76eda0909.png", "email": "admin@admin.com", "mobile": "", "loginfailure": 0, "logintime": 1713837105, "loginip": "36.57.139.161", "createtime": 1491635035, "updatetime": 1713837105, "token": "fdbeab5b-c1e6-4813-ae22-62972f3a5075", "status": "normal", "role": "", "roleid": 0, "commission": "0.00" }');
    },
    methods: {
        init() {
            // this.getSite();
            if (!storage.getItem("siteName") || !storage.getItem("logoImg") || !storage.getItem(
                "sitelogo_expiration_time")) {
                this.getSite();
            } else {
                // 如果缓存过期，则获取最新的信息
                if (new Date() > storage.getItem("sitelogo_expiration_time")) {
                    this.getSite();
                    return;
                } else {
                    window.document.title = storage.getItem("siteName");
                    //动态获取icon
                    let link = document.querySelector("link[rel*='icon']") || document.createElement("link");
                    link.type = "image/x-icon";
                    link.href = storage.getItem("siteIcon");
                    link.rel = "shortcut icon";
                    document.getElementsByTagName("head")[0].appendChild(link);
                }
            }

            this.$router.onReady(() => {
                let purchase = { type: '', id: '' };
                if (this.$route.query.purchase_id) {
                    purchase['type'] = 'purchase';
                    purchase['id'] = this.$route.query.purchase_id;
                } else if (this.$route.query.tender_id) {
                    purchase['type'] = 'tender';
                    purchase['id'] = this.$route.query.tender_id;
                }

                if (this.$route.name == 'Index') {
                    storage.setItem('PURCHASE', purchase);
                }
            })
        },
        getSite() {
            //获取基本站点信息
            getBaseSite().then((res) => {
                if (res.code == 1 && res.data) {
                    let data = res.data;
                    // 过期时间
                    var expirationTime = new Date().setHours(new Date().getHours() + 1);
                    // 存放过期时间
                    storage.setItem("sitelogo_expiration_time", expirationTime);
                    // 存放信息
                    storage.setItem('siteName', data.siteName);
                    storage.setItem('logoImg', data.siteLogo);
                    storage.setItem("siteIcon", data.siteIcon);
                    window.document.title = data.siteName;
                    //动态获取icon
                    let link = document.querySelector("link[rel*='icon']") || document.createElement("link");
                    link.type = "image/x-icon";
                    link.href = data.siteIcon;
                    link.rel = "shortcut icon";
                    document.getElementsByTagName("head")[0].appendChild(link);
                }
            });

            //获取用户登录状态
            // getAdminState({ username: 'admin', password: 'admin@123789' }).then((res) => {
            //     console.log(res, 'app');
            //     if (res.code === 0) {
            //         //window.location.href = 'http://gongyinlian.com:8089/zWQuofSZCn.php/index/login';
            //         var i = getAdminLoginUrl();
            //     } else {
            //         let admin = res.data;
            //         // console.log(admin)
            //         // storage.setItem("userInfo", {"code":1,"msg":"success","time":"1713498704","data":{"id":37,"username":"syl","nickname":"Selena  SU","password":"e5316fb4379590cda370d7e21b2cef7a","salt":"bVdcl1","avatar":"\/uploads\/20230718\/a5a0273358431e51583f09cb4ac867eb.png","email":"402465925@qq.com","mobile":"13965061331","loginfailure":0,"logintime":1713494691,"loginip":"114.102.152.2","createtime":1681653835,"updatetime":1713494691,"token":"986e106a-3093-4e2a-8ae3-3d6a155ecd80","status":"normal","role":"","roleid":0,"commission":"0.00"}});
            //         storage.setItem("userInfo", admin);
            //     }
            // });
        }
    }
};
</script>
<style lang="scss">
#app {
    @include background_color($light_background_color);
}
</style>
